package com.vm.myview;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.vm.activity.BitmapCache;
import com.vm.activity.DetailActivity;
import com.vm.commons.VMConst;
import com.vm.model.ActiveSoft;
import com.wwt.app.R;

/**
 * 
 * @ProjectName: [WeiMai]
 * @Author: [kent.wang]
 * @CreateDate: [2015年3月27日 下午2:17:00]
 * @Version: [v1.0]
 * 
 */
public class ViewDownUtils {

	private Context mContext;
	private View view;
	Activity activity;

	private RequestQueue mQueue;
	ImageListener listener;
	ImageLoader mImageLoader;

	public ViewDownUtils(Context context, Activity activity) {
		this.mContext = context;
		this.activity = activity;

		mQueue = Volley.newRequestQueue(context);
	}

	/**
	 * 动态创建LinearLayout
	 * 
	 * @param i
	 * @param activeSoft
	 * @param progress
	 *            1.初始化进度条,0不初始化进度条
	 * @return
	 */
	public LinearLayout dyAddView(int i, final ActiveSoft activeSoft,
			int progress) {

		LinearLayout linearLayout = (LinearLayout) activity.getLayoutInflater()
				.inflate(R.layout.found_item, null);
		if (i == 0) {
			mImageLoader = new ImageLoader(mQueue, new BitmapCache());
		}


		RelativeLayout rlayout_info = (RelativeLayout) linearLayout
				.findViewById(R.id.rlayout_info);
		RelativeLayout rlayout_progressBar = (RelativeLayout) linearLayout
				.findViewById(R.id.rlayout_progressBar);

		TextView tv_softName = (TextView) linearLayout
				.findViewById(R.id.tv_softName);
		ImageView iv_softImage = (ImageView) linearLayout
				.findViewById(R.id.iv_softImage);
		TextView tv_softSize = (TextView) linearLayout
				.findViewById(R.id.tv_softSize);
		RatingBar rb_star = (RatingBar) linearLayout.findViewById(R.id.rb_star);

		if (activeSoft.getName() != null) {
			// String softName =
			// StringUtil.getShotName(soft.getName());
			tv_softName.setText(activeSoft.getName());
		}
		// 大小
		if (activeSoft.getSize() != null) {
			float size = activeSoft.getSize();
			tv_softSize.setText(Float.toString(size) + "M");
		}
		// 星级
		if (activeSoft.getStar() != null) {
			float fstar = activeSoft.getStar();
			rb_star.setRating(fstar / 2);
		}

		listener = ImageLoader
				.getImageListener(iv_softImage,
						android.R.drawable.ic_menu_rotate,
						android.R.drawable.ic_delete);

		mImageLoader.get(VMConst.URL + activeSoft.getImageUrl(), listener);

		OnClickListener ll_listener = new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(activity, DetailActivity.class);

				intent.putExtra("softId", activeSoft.getSoftId());
				activity.startActivityForResult(intent, 10);
			}
		};

		linearLayout.setOnClickListener(ll_listener);

		/**
		 * 加载一些列下载有关的东西
		 */

		rlayout_info.setVisibility(View.GONE);
		rlayout_progressBar.setVisibility(View.VISIBLE);
		new DownloadProgress(activity, linearLayout, activeSoft);

		// downloadService.initProgressBar(soft);
		return linearLayout;
	}

}
